// pages/tiku/fenlei.js
//获取应用实例
const app = getApp()
var video_admin = null;//管理员权限
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shijuan: [],
    pes_video: [],
    thcl: '',
    station_type: ''
  },
  //点击开始学习按钮，进入视频列表界面
  startprsvideo: function (event) {
    //视频任务主表ID
    var learnVideoTaskId = event.currentTarget.dataset.pesid;
    //管理时长
    var manager = event.currentTarget.dataset.manager;
    //基础时长
    var base = event.currentTarget.dataset.base;
    //console.log(pesid)
    //允许学习开始日期
    var startdate = event.currentTarget.dataset.startdate;
    //console.log('允许考试日期'+startdate);
    //var startdate1 = startdate+' 00:00:00';
    //允许学习结束日期
    var enddate = event.currentTarget.dataset.enddate;
    //var enddate1 = enddate+' 23:59:59';
    var beginDate = new Date(startdate);
    var endDate = new Date(enddate);

    try {
      //var value = wx.getStorageSync('key')
      //缓存获取用户数据
      var empinfo = wx.getStorageSync('empinfo');
      if (empinfo) {
          var stationType = empinfo.stationType;
            //当前日期
          var date = new Date();
          //console.log('当前日期'+date)
          //管理员权限赋值
          video_admin = empinfo.adminMobile;
          //管理员
          if(video_admin != null){
            //console.log('===='+video_admin)
            //插入视频主表和子表1
            app.formPost('/wx/video/insert_video_record', {
              employeeId: empinfo.employeeId,
              learnVideoTaskId: learnVideoTaskId,
              manager: manager,
              base: base,
              stationType: stationType,
            }).then(res => {
              //console.log(res.tests)
              if (res.code == 200) {
                //进入视频学习界面1
                wx.navigateTo({
                  //url: '/pages/tiku/kaoti_timu?id=' + learnVideoTaskId ,
                  url: '/pages/tiku/list?id=' + learnVideoTaskId ,
                })
              }
            }).catch(e => {
              wx.showModal({
                title: e
              })
            })
          }else{
            if ((date - beginDate) > 0 && (date - endDate) < 0){
              //插入视频主表和子表1
              app.formPost('/wx/video/insert_video_record', {
                employeeId: empinfo.employeeId,
                learnVideoTaskId: learnVideoTaskId,
                manager: manager,
                base: base,
                stationType: stationType,
              }).then(res => {
                //console.log(res.tests)
                if (res.code == 200) {
                  //进入视频学习界面1
                  wx.navigateTo({
                    //url: '/pages/tiku/kaoti_timu?id=' + learnVideoTaskId ,
                    url: '/pages/tiku/list?id=' + learnVideoTaskId ,
                  })
                }
              }).catch(e => {
                wx.showModal({
                  title: e
                })
              })
            }else{
              wx.showToast({
                title: '学习开始日期为:' + startdate + ';\r\n学习结束日期为:' + enddate + ';\r\n请在学习日期段进行学习！',
                icon: 'none',
                duration: 2000
              })
            }
          }
      }else{
        wx.reLaunch({
          url: '/pages/user/bind/index',
        });
      }
    } catch (e) {
      wx.reLaunch({
        url: '/pages/user/bind/index',
      });
    }
  },
  //开始考试
  startkaoshi: function (event) {
     // var that = this;
      //试卷主表ID
      var testid = event.currentTarget.dataset.testid;
      //视频任务主表ID
      var learn_video_task_id = event.currentTarget.dataset.videoid;
      //console.log('点击开始考试时的视频任务ID=='+learn_video_task_id)
      //允许考试次数
      var examtimes = event.currentTarget.dataset.examtimes;
      //允许考试开始日期2021-12-21
      var startdate = event.currentTarget.dataset.startdate;
      //var startdate1 = startdate+' 00:00:00';
      //var startdate1 = '2021/12/21'+' 00:00:00';
      //允许考试结束日期
      var enddate = event.currentTarget.dataset.enddate;
      //var enddate1 = enddate+' 23:59:59';
      //var enddate1 = '2021/12/31'+' 23:59:59';

      var beginDate = new Date(startdate);
      
      var endDate = new Date(enddate);
      //var endDate2 = new Date(new Date((endDate1 / 1000 + 86400) * 1000));
      //及格分数
      var passscore = event.currentTarget.dataset.passscore;
      //console.log('及格分数为:'+passscore)
      //考试时长
      var examtime = event.currentTarget.dataset.examtime;
      var name = event.currentTarget.dataset.name;
      //console.log(name)
      var date = new Date();
      //判断是否超过考试次数次数和及格分数
      //var empinfo = wx.getStorageSync('empinfo')
      //console.log(empinfo)
      try {
        var empinfo = wx.getStorageSync('empinfo')
        if (empinfo) {
          var csry = empinfo.mobile;
          var companyId = empinfo.companyId;
          //管理员权限赋值
          video_admin = empinfo.adminMobile;
            if(csry == '18033889005'){
              app.formPost('/wx/test/get-transcript-sum', {
                employeeId: empinfo.employeeId,
                testpaper_id: testid
              }).then(res => {
                if (res.code == 200) {
                  var nowExamTimes = res.transcriptSum.examTimes + 1;
                  //console.log('本次考试次数'+nowExamTimes)1
                  if (res.transcriptSum.examTimes >= examtimes) {
                    wx.showToast({
                      title: '您已超出此考试的最大次数，无法再次考试！',
                      icon: 'none',
                      duration: 2000
                    })
                  } 
                  // else if (res.transcriptSum.excamMark >= passscore) {
                  //   wx.showToast({
                  //     title: '此考试您已经及格，不能再次考试！',
                  //     icon: 'none',
                  //     duration: 2000
                  //   })
                  // } 
                  else {
                    if ((date - beginDate) > 0 && (date - endDate) < 0) {
                      wx.showModal({
                        //title: '提示',
                        content: '本次为您第' + nowExamTimes + '次考试\r\n考试时长' + examtime + '分钟\r\n允许考试次数' + examtimes + '次\r\n及格分数' + passscore + '\r\n确定开始考试吗？',
                        success(res) {
                          if (res.confirm) {
                            //考试结束时间
                            var date1 = new Date().getTime() + examtime * 60 * 1000;
                            var date2 = new Date(date1);
                            var endTmpTime = wx.getStorageSync('endTmpTime')
                            if (endTmpTime) {
                              date2 = endTmpTime;
                              //console.log('缓存中的:' + date2)
                            } else {
                              //第一次点击开始考试
                              wx.setStorageSync('endTmpTime', date2)
                            // console.log('第一次点击开始考试的:' + date2)
                            }
                            //进入考试界面
                            var starttime = Date.parse(new Date());
                            wx.navigateTo({
                              url: '/pages/tiku/kaoti_index?testid=' + testid + '&endtime=' + date2 + '&starttime=' + starttime + '&testname=' + name,
                            })
                          }
                        }
                      })
                    } else {
                      wx.showToast({
                        title: '考试开始日期为:' + startdate + ';\r\n考试结束日期为:' + enddate + ';\r\n请在考试日期段进行考试！',
                        icon: 'none',
                        duration: 2000
                      })
                    }
                  }
                } else if (res.code == 201) {
                  if ((date - beginDate) > 0 && (date - endDate) < 0) {
                    wx.showModal({
                      //title: '提示',
                      content: '本次为您第1次考试\r\n考试时长' + examtime + '分钟\r\n允许考试次数' + examtimes + '次\r\n及格分数' + passscore + '\r\n确定开始考试吗？',
                      success(res) {
                        if (res.confirm) {
                          //首次考试，加油
                          //考试结束时间
                          var date1 = new Date().getTime() + examtime * 60 * 1000;
                          var date2 = new Date(date1);
                          var endTmpTime = wx.getStorageSync('endTmpTime')
                          if (endTmpTime) {
                            date2 = endTmpTime;
                            //console.log('缓存中的:' + date2)
                          } else {
                            //第一次点击开始考试
                            wx.setStorageSync('endTmpTime', date2)
                            //console.log('第一次点击开始考试的:' + date2)
                          }
                          //进入考试界面
                          var starttime = Date.parse(new Date());
                          wx.navigateTo({
                            url: '/pages/tiku/kaoti_index?testid=' + testid + '&endtime=' + date2 + '&starttime=' + starttime + '&testname=' + name,
                          })
                        }
                      }
                    })
                  } else {
                    wx.showToast({
                      title: '考试开始日期为:' + startdate + ';\r\n考试结束日期为:' + enddate + ';\r\n请在考试日期段进行考试！',
                      icon: 'none',
                      duration: 2000
                    })
                  }
        
                }
              }).catch(e => {
                wx.showModal({
                  title: e
                })
              })
            }else{
              if(learn_video_task_id == '' || learn_video_task_id == null){
                app.formPost('/wx/test/get-transcript-sum', {
                  employeeId: empinfo.employeeId,
                  testpaper_id: testid
                }).then(res => {
                  if (res.code == 200) {
                    var nowExamTimes = res.transcriptSum.examTimes + 1;
                    //console.log('本次考试次数'+nowExamTimes)
                    if (res.transcriptSum.examTimes >= examtimes) {
                      wx.showToast({
                        title: '您已超出此考试的最大次数，无法再次考试！',
                        icon: 'none',
                        duration: 2000
                      })
                    } else {
                      if ((date - beginDate) > 0 && (date - endDate) < 0) {
                        wx.showModal({
                          //title: '提示',
                          content: '本次为您第' + nowExamTimes + '次考试\r\n考试时长' + examtime + '分钟\r\n允许考试次数' + examtimes + '次\r\n及格分数' + passscore + '\r\n确定开始考试吗？',
                          success(res) {
                            if (res.confirm) {
                              //考试结束时间
                              var date1 = new Date().getTime() + examtime * 60 * 1000;
                              var date2 = new Date(date1);
                              var endTmpTime = wx.getStorageSync('endTmpTime')
                              if (endTmpTime) {
                                date2 = endTmpTime;
                                //console.log('缓存中的:' + date2)
                              } else {
                                //第一次点击开始考试
                                wx.setStorageSync('endTmpTime', date2)
                              // console.log('第一次点击开始考试的:' + date2)
                              }
                              //进入考试界面
                              var starttime = Date.parse(new Date());
                              wx.navigateTo({
                                url: '/pages/tiku/kaoti_index?testid=' + testid + '&endtime=' + date2 + '&starttime=' + starttime + '&testname=' + name,
                              })
                            }
                          }
                        })
                      } else {
                        var title1 = '考试开始日期为:' + startdate + '考试结束日期为:' + enddate + '请在考试日期段进行考试！'
                        wx.showToast({
                          title: title1,
                          //title: '考试开始日期为:' + startdate + ';\r\n考试结束日期为:' + enddate + ';\r\n请在考试日期段进行考试！',
                          icon: 'none',
                          duration: 2000
                        })
                      }
                    }
                  } else if (res.code == 201) {
                    if ((date - beginDate) > 0 && (date - endDate) < 0) {
                      wx.showModal({
                        //title: '提示',
                        content: '本次为您第1次考试\r\n考试时长' + examtime + '分钟\r\n允许考试次数' + examtimes + '次\r\n及格分数' + passscore + '\r\n确定开始考试吗？',
                        success(res) {
                          if (res.confirm) {
                            //首次考试，加油
                            //考试结束时间
                            var date1 = new Date().getTime() + examtime * 60 * 1000;
                            var date2 = new Date(date1);
                            var endTmpTime = wx.getStorageSync('endTmpTime')
                            if (endTmpTime) {
                              date2 = endTmpTime;
                              //console.log('缓存中的:' + date2)
                            } else {
                              //第一次点击开始考试
                              wx.setStorageSync('endTmpTime', date2)
                              //console.log('第一次点击开始考试的:' + date2)
                            }
                            //进入考试界面
                            var starttime = Date.parse(new Date());
                            wx.navigateTo({
                              url: '/pages/tiku/kaoti_index?testid=' + testid + '&endtime=' + date2 + '&starttime=' + starttime + '&testname=' + name,
                            })
                          }
                        }
                      })
                    } else {
                      wx.showToast({
                        title: '考试开始日期为:' + startdate + ';\r\n考试结束日期为:' + enddate + ';\r\n请在考试日期段进行考试！',
                        icon: 'none',
                        duration: 2000
                      })
                    }
          
                  }
                }).catch(e => {
                  wx.showModal({
                    title: e
                  })
                })
              }else{
                //获取视频数据
                  app.formPost('/wx/video/video_is_end', {
                    'employeeId': empinfo.employeeId,
                    'learnVideoTaskId': learn_video_task_id,
                  }).then(res => {
                    if (res.code == 200) {
                      var isEnd = res.is_end;
                      //console.log('1111===='+isEnd)
                      //管理员
                      if(video_admin != null){
                        app.formPost('/wx/test/get-transcript-sum', {
                          employeeId: empinfo.employeeId,
                          testpaper_id: testid
                        }).then(res => {
                          if (res.code == 200) {
                            var nowExamTimes = res.transcriptSum.examTimes + 1;
                            //console.log('本次考试次数'+nowExamTimes)
                            if (res.transcriptSum.examTimes >= examtimes) {
                              wx.showToast({
                                title: '您已超出此考试的最大次数，无法再次考试！',
                                icon: 'none',
                                duration: 2000
                              })
                            } 
                            // else if (res.transcriptSum.excamMark >= passscore) {
                            //   wx.showToast({
                            //     title: '此考试您已经及格，不能再次考试！',
                            //     icon: 'none',
                            //     duration: 2000
                            //   })
                            // } 
                            else {
                              if ((date - beginDate) > 0 && (date - endDate) < 0) {
                                wx.showModal({
                                  //title: '提示',
                                  content: '本次为您第' + nowExamTimes + '次考试\r\n考试时长' + examtime + '分钟\r\n允许考试次数' + examtimes + '次\r\n及格分数' + passscore + '\r\n确定开始考试吗？',
                                  success(res) {
                                    if (res.confirm) {
                                      //考试结束时间
                                      var date1 = new Date().getTime() + examtime * 60 * 1000;
                                      var date2 = new Date(date1);
                                      var endTmpTime = wx.getStorageSync('endTmpTime')
                                      if (endTmpTime) {
                                        date2 = endTmpTime;
                                        //console.log('缓存中的:' + date2)
                                      } else {
                                        //第一次点击开始考试
                                        wx.setStorageSync('endTmpTime', date2)
                                      // console.log('第一次点击开始考试的:' + date2)
                                      }
                                      //进入考试界面
                                      var starttime = Date.parse(new Date());
                                      wx.navigateTo({
                                        url: '/pages/tiku/kaoti_index?testid=' + testid + '&endtime=' + date2 + '&starttime=' + starttime + '&testname=' + name,
                                      })
                                    }
                                  }
                                })
                              } else {
                                wx.showToast({
                                  title: '考试开始日期为:' + startdate + ';\r\n考试结束日期为:' + enddate + ';\r\n请在考试日期段进行考试！',
                                  icon: 'none',
                                  duration: 2000
                                })
                              }
                            }
                          } else if (res.code == 201) {
                            if ((date - beginDate) > 0 && (date - endDate) < 0) {
                              wx.showModal({
                                //title: '提示'1,
                                content: '本次为您第1次考试\r\n考试时长' + examtime + '分钟\r\n允许考试次数' + examtimes + '次\r\n及格分数' + passscore + '\r\n确定开始考试吗？',
                                success(res) {
                                  if (res.confirm) {
                                    //首次考试，加油
                                    //考试结束时间
                                    var date1 = new Date().getTime() + examtime * 60 * 1000;
                                    var date2 = new Date(date1);
                                    var endTmpTime = wx.getStorageSync('endTmpTime')
                                    if (endTmpTime) {
                                      date2 = endTmpTime;
                                      //console.log('缓存中的:' + date2)
                                    } else {
                                      //第一次点击开始考试
                                      wx.setStorageSync('endTmpTime', date2)
                                      //console.log('第一次点击开始考试的:' + date2)
                                    }
                                    //进入考试界面
                                    var starttime = Date.parse(new Date());
                                    wx.navigateTo({
                                      url: '/pages/tiku/kaoti_index?testid=' + testid + '&endtime=' + date2 + '&starttime=' + starttime + '&testname=' + name,
                                    })
                                  }
                                }
                              })
                            } else {
                              wx.showToast({
                                title: '考试开始日期为:' + startdate + ';\r\n考试结束日期为:' + enddate + ';\r\n请在考试日期段进行考试！',
                                icon: 'none',
                                duration: 2000
                              })
                            }
                  
                          }
                        }).catch(e => {
                          wx.showModal({
                            title: e
                          })
                        })
                      }else {
                        //非管理员
                        if(isEnd != 1){
                          wx.showToast({
                            title: '您还未学习完视频课程，无法进行考试！',
                            icon: 'none',
                            duration: 2000
                          })
                        }else{
                          //console.log('2222===='+isEnd)
                            app.formPost('/wx/test/get-transcript-sum', {
                              employeeId: empinfo.employeeId,
                              testpaper_id: testid
                            }).then(res => {
                              if (res.code == 200) {
                                var nowExamTimes = res.transcriptSum.examTimes + 1;
                                //console.log('本次考试次数'+nowExamTimes)
                                if (res.transcriptSum.examTimes >= examtimes) {
                                  wx.showToast({
                                    title: '您已超出此考试的最大次数，无法再次考试！',
                                    icon: 'none',
                                    duration: 2000
                                  })
                                } 
                                // else if (res.transcriptSum.excamMark >= passscore) {
                                //   wx.showToast({
                                //     title: '此考试您已经及格，不能再次考试！',
                                //     icon: 'none',
                                //     duration: 2000
                                //   })
                                // } 
                                else {
                                  if ((date - beginDate) > 0 && (date - endDate) < 0) {
                                    wx.showModal({
                                      //title: '提示',
                                      content: '本次为您第' + nowExamTimes + '次考试\r\n考试时长' + examtime + '分钟\r\n允许考试次数' + examtimes + '次\r\n及格分数' + passscore + '\r\n确定开始考试吗？',
                                      success(res) {
                                        if (res.confirm) {
                                          //考试结束时间
                                          var date1 = new Date().getTime() + examtime * 60 * 1000;
                                          var date2 = new Date(date1);
                                          var endTmpTime = wx.getStorageSync('endTmpTime')
                                          if (endTmpTime) {
                                            date2 = endTmpTime;
                                            //console.log('缓存中的:' + date2)
                                          } else {
                                            //第一次点击开始考试
                                            wx.setStorageSync('endTmpTime', date2)
                                          // console.log('第一次点击开始考试的:' + date2)
                                          }
                                          //进入考试界面
                                          var starttime = Date.parse(new Date());
                                          wx.navigateTo({
                                            url: '/pages/tiku/kaoti_index?testid=' + testid + '&endtime=' + date2 + '&starttime=' + starttime + '&testname=' + name,
                                          })
                                        }
                                      }
                                    })
                                  } else {
                                    wx.showToast({
                                      title: '考试开始日期为:' + startdate + ';\r\n考试结束日期为:' + enddate + ';\r\n请在考试日期段进行考试！',
                                      icon: 'none',
                                      duration: 2000
                                    })
                                  }
                                }
                              } else if (res.code == 201) {
                                if ((date - beginDate) > 0 && (date - endDate) < 0) {
                                  wx.showModal({
                                    //title: '提示',
                                    content: '本次为您第1次考试\r\n考试时长' + examtime + '分钟\r\n允许考试次数' + examtimes + '次\r\n及格分数' + passscore + '\r\n确定开始考试吗？',
                                    success(res) {
                                      if (res.confirm) {
                                        //首次考试，加油
                                        //考试结束时间
                                        var date1 = new Date().getTime() + examtime * 60 * 1000;
                                        var date2 = new Date(date1);
                                        var endTmpTime = wx.getStorageSync('endTmpTime')
                                        if (endTmpTime) {
                                          date2 = endTmpTime;
                                          //console.log('缓存中的:' + date2)
                                        } else {
                                          //第一次点击开始考试
                                          wx.setStorageSync('endTmpTime', date2)
                                          //console.log('第一次点击开始考试的:' + date2)
                                        }
                                        //进入考试界面
                                        var starttime = Date.parse(new Date());
                                        wx.navigateTo({
                                          url: '/pages/tiku/kaoti_index?testid=' + testid + '&endtime=' + date2 + '&starttime=' + starttime + '&testname=' + name,
                                        })
                                      }
                                    }
                                  })
                                } else {
                                  wx.showToast({
                                    title: '考试开始日期为:' + startdate + ';\r\n考试结束日期为:' + enddate + ';\r\n请在考试日期段进行考试！',
                                    icon: 'none',
                                    duration: 2000
                                  })
                                }
                      
                              }
                            }).catch(e => {
                              wx.showModal({
                                title: e
                              })
                            })
                        }
                      }
                      
                    }
                }).catch(e => {
                    wx.showModal({
                    title: e
                    })
                })
              }
              
            }
        }else{
          wx.reLaunch({
            url: '/pages/user/bind/index',
          });
        }
      } catch (e) {
        wx.reLaunch({
          url: '/pages/user/bind/index',
        });
      }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    try {
      var empinfo = wx.getStorageSync('empinfo');
      if (empinfo) {
        var csry = empinfo.mobile;
      that.setData({
        thcl: csry,
        //用户类型赋值
        station_type: empinfo.stationType
      }),
      //获取视频数据
      app.formPost('/wx/video/all-video', {
        'companyId': empinfo.companyId,
      }).then(res => {
        //console.log(res.videos)
        if (res.code == 200) {
          that.setData({
            pes_video: res.pes_videos
          })
        }
      }).catch(e => {
        wx.showModal({
          title: e
        })
      })
        //获取试卷数据
        app.formPost('/wx/test/all-test', {
          'companyId': empinfo.companyId,
        }).then(res => {
          //console.log(res.tests)
          if (res.code == 200) {
            that.setData({
              shijuan: res.tests
            })
          }
        }).catch(e => {
          wx.showModal({
            title: e
          })
        })
      }else{
        wx.reLaunch({
          url: '/pages/user/bind/index',
        });
      }
    } catch (e) {
      wx.reLaunch({
        url: '/pages/user/bind/index',
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    //var token = wx.getStorageSync('token');
    
    try {
      var empinfo = wx.getStorageSync('empinfo');
      if (empinfo) {
        var csry = empinfo.mobile;
        that.setData({
          thcl: csry,
          //用户类型赋值
          station_type: empinfo.stationType,
        })
      }else{
        wx.reLaunch({
          url: '/pages/user/bind/index',
        });
      }
    } catch (e) {
      wx.reLaunch({
        url: '/pages/user/bind/index',
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})